<template>
    <div class="container">
        <div class="table" v-if="show===0">
            <div class="add-button">
            <el-button type="primary" @click="add">新增活动</el-button>
        </div>
        <el-table :data="dataList" style="width: 100%" border>
            <el-table-column type="index" label="序号" width="80px">
            </el-table-column>
            <el-table-column prop="title" label="标题" >
            </el-table-column>
            <el-table-column prop="time" label="活动时间" >
            </el-table-column>
            <el-table-column prop="place" label="活动地点" >
            </el-table-column>
            <el-table-column  label="操作" width="280px">
                <template slot-scope="scope">
                <!-- <div class="btn-group"> -->
                    <el-button type="warning" icon="el-icon-info" size="mini" @click="detail(scope.row.id)">详情</el-button>
                    <el-button type="primary" icon="el-icon-edit" size="mini" @click="edit(scope.row.id)">修改</el-button>
                    <el-button type="danger" icon="el-icon-delete" size="mini" @click="del(scope.row.id)">删除</el-button>
                <!-- </div> -->
            </template>
            </el-table-column>
        </el-table>
        <div class="pagination">
            <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="queryInfo.pageNum"
                :page-sizes="[5, 10, 20, 50]"
                :page-size=queryInfo.pageSize
                layout="total, sizes, prev, pager, next, jumper"
                :total=total>
                </el-pagination>
        </div>
        </div>
        <div class="add-form" v-if="show===1">
            <el-card style="padding-bottom: 20px;">
                <div slot="header">新增活动</div>
                <div>
                    <el-form :model="addForm" label-width="120px" :rules="addRules" ref="addFormRef">
                        <el-form-item label="标题" prop="title">
                            <el-input v-model="addForm.title"></el-input>
                        </el-form-item> 
                        <el-form-item label="活动地点" prop="place">
                            <el-input v-model="addForm.place"></el-input>
                        </el-form-item>
                        <el-form-item label="活动时间" prop="time">
                            <el-date-picker
                                v-model="addForm.time"
                                type="datetime"
                                placeholder="选择日期时间">
                            </el-date-picker>
                        </el-form-item>
                        <el-form-item label="添加到知识库"> 
                            <el-upload
                            class="upload-demo"
                            action="http://localhost:9000/knowledgeBase/uploadKnowledgeBase"
                            :on-preview="handlePreview"
                            :on-remove="handleRemove"
                            :before-remove="beforeRemove"
                            multiple
                            :limit="3"
                            :on-exceed="handleExceed"
                            :file-list="fileList">
                            <el-button size="small" type="primary">点击上传</el-button>
                            </el-upload>
                        </el-form-item>
                        <el-form-item label="内容" prop="content">
                            <QuillEditor :content="addForm.content" @contentChange="getPrivacyAgreement"></QuillEditor>
                            <!-- <el-input v-model="addForm.content" type="textarea" :rows="5"></el-input> -->
                       </el-form-item>
                    </el-form>
                </div>
                <div style="float: right;">
                    <el-button type="info" @click="cancelAdd">取消</el-button>
                    <el-button type="primary" @click="SureAdd">发布</el-button>
                </div>
            </el-card>
        </div>
        <div class="edit-form" v-if="show===2">
            <el-card style="padding-bottom: 20px;">
                <div slot="header">修改活动</div>
                <div>
                    <el-form :model="editForm" label-width="120px" :rules="addRules" ref="addFormRef">
                        <el-form-item label="标题" prop="title">
                            <el-input v-model="editForm.title"></el-input>
                        </el-form-item>
                        <el-form-item label="活动时间" prop="time">
                                <el-date-picker
                                v-model="editForm.time"
                                type="datetime"
                                placeholder="选择日期时间">
                            </el-date-picker>
                        </el-form-item>
                        <el-form-item label="活动地点" prop="place">
                            <el-input v-model="editForm.place"></el-input>
                        </el-form-item>
                        <el-form-item label="添加到知识库"> 
                            <el-upload
                            class="upload-demo"
                            action="http://localhost:9000/knowledgeBase/uploadKnowledgeBase"
                            :on-preview="handlePreview"
                            :on-remove="handleRemove"
                            :before-remove="beforeRemove"
                            multiple
                            :limit="3"
                            :on-exceed="handleExceed"
                            :file-list="fileList">
                            <el-button size="small" type="primary">点击上传</el-button>
                            </el-upload>
                        </el-form-item>
                        <el-form-item label="内容" prop="content">
                            <QuillEditor :content="editForm.content" @contentChange="getPrivacyAgreement"></QuillEditor>
                            <!-- <el-input v-model="addForm.content" type="textarea" :rows="5"></el-input> -->
                       </el-form-item>
                    </el-form>
                </div>
                <div style="float: right;">
                    <el-button type="info" @click="cancelEdit">取消</el-button>
                    <el-button type="primary" @click="SureEdit">发布</el-button>
                </div>
            </el-card>
        </div>
        <div v-if="show===3">
            <el-card>
                <div class="back">
                        <i class="el-icon-back" @click="back">返回</i>
                    </div>
                <div>
                    <div class="detail-header">
                    {{detailForm.title}}
                </div>
                <div class="detail-time">
                        <span>活动时间：</span>
                        <span>{{ detailForm.time }}</span>
                    </div>
                <div v-html="detailForm.content" class="detail-content"></div>
                </div>
            </el-card>
        </div>
    </div>
</template>
<script>
import QuillEditor from '@/components/QuillEditor.vue';
import {formatTimestampWithOffset} from '@/utils/formatTimestampWithOffset.js'
export default {
    components:{QuillEditor},
    data(){
        return {
            queryInfo:{
                pageNum:1,
                pageSize:10
            },
            dataList:[],
            total:0,
            show:0,
            addForm:{},
            editForm:{},
            detailForm:{},
            addRules:{
                title:[
                    {required:true,message:'请输入标题',trigger:'blur'}
                ],
                place:[
                    {required:true,message:'请输入活动地点',trigger:'blur'}
                ],
                time:[
                    {required:true,message:'请输入活动时间',trigger:'blur'}
                ],
                content:[
                    {required:true,message:'请输入内容',trigger:'blur'}
                ]
            }
        }
    },
    methods:{
        //获取所有通知新
        async getAllActivitiesOrders(){
            const {data:res} = await this.$http.post("/getAllActivitiesOrderList",this.queryInfo );
            if(res.code === 200){
                this.dataList = res.data;
                for(let item of this.dataList){
                    item.time = formatTimestampWithOffset(item.time);
                }
                this.total = res.count;
            }
        },
        handleSizeChange(newSize){
            this.queryInfo.pageSize = newSize;
            this.getAllActivitiesOrders();
        },
        handleCurrentChange(newPage){            
            this.queryInfo.pageNum = newPage;
            this.getAllActivitiesOrders();
        },
        getPrivacyAgreement(val) {
        if(this.show === 1){
            this.addForm.content = val;
        }else if(this.show === 2){
            this.editForm.content = val;
        }
        },
        add(){
            this.show = 1;
        },
        async SureAdd(){
            this.addForm.time = new Date(this.addForm.time).toISOString();
            this.$refs.addFormRef.validate(async valid=>{
                const {data:res} = await this.$http.post('/addActivitiesOrder',this.addForm)
                if(res.code === 200){
                    this.$message.success(res.message);
                }else{
                    this.$message.error(res.message);
                }
                this.show = 0;
                this.getAllActivitiesOrders()
                this.addForm = {}
            })
        },
        cancelAdd(){
            this.addForm = {}
            this.show = 0;
        },

        async edit(id){
            const {data:res} = await this.$http.get('/getActivitiesOrder?id='+id);
            if(res.code === 200){
                this.show = 2
                this.editForm = res.data;
                this.editForm.time = formatTimestampWithOffset(this.editForm.time);
            } else {
                this.$message.error(res.message);
            }
        },
        async SureEdit(){
            const {data:res} = await this.$http.post('/updateActivitiesOrder',this.editForm);
            if(res.code === 200){
                this.$message.success(res.message);
            }else{
                this.$message.error(res.message);
            }
            this.editForm = {};
            this.show = 0;
            this.getAllActivitiesOrders();
        },
        cancelEdit(){
            this.editForm = {};
            this.show = 0;
        },
        async del(id){
            const {data:res} = await this.$http.delete('/deleteActivitiesOrder?id='+id);
            if(res.code === 200){
                this.$message.success(res.message);
            }else{
                this.$message.error(res.message);
            }
            this.getAllActivitiesOrders();
        },        
        async detail(id){
            const {data:res} = await this.$http.get('/getActivitiesOrder?id='+id);
            if(res.code === 200){
                this.detailForm = res.data;
                this.detailForm.time = formatTimestampWithOffset(this.detailForm.time);
                this.$message.success(res.message);
                this.show = 3;
            }else {
                this.$message.error(res.message);
            }
        },
        back(){
            this.show = 0;
            this.detailForm = {}
        },
    },
    created(){
        this.getAllActivitiesOrders();
    }
}
</script>
<style lang="less" scoped>
.add-button{
    float: right;
    margin-bottom: 10px;
}
.el-table{
    align-items: center;
}
.pagination{
    float: right;
}
.btn-group{
    display: flex;
}
.detail-header{
    display: flex;
    justify-content: center;
    align-items: center;
    width: 100%;
}
.detail-time{
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 12px;
    color: gray;
    padding: 5px;
}
.detail-content{
    height: 100vh;
    overflow-y: auto;
    padding: 0 30px 20px 30px;
    font-size: 18px;
    color: rgb(97, 97, 97);
}
.back{
    font-size: 14px;
    color: gray;
    cursor: pointer;
}
</style>